package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Banco;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.service.BancoService;
import edu.upc.clase.demo.service.EstadoService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Carlos Ríos Rosales
 */
@Controller("bancoController")
public class BancoController {

  @Autowired
  private BancoService bancoService;
  @Autowired
  private EstadoService estadoService;
  private static Logger log = LoggerFactory.getLogger(BancoController.class);

  @RequestMapping("/Banco/index")
  public ModelAndView index() {
    ModelAndView mav = new ModelAndView("Banco/index");
    List<Banco> Bancos = bancoService.buscarTodos();
    CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
    mav.addObject("banco", Bancos);
    mav.addObject("criterioBusqueda", criterioBusqueda);
    return mav;
  }

  @RequestMapping(value = "/Banco/new", method = RequestMethod.GET)
  public ModelAndView newBanco() {
    ModelAndView mav = new ModelAndView("Banco/new");
    Banco banco = new Banco();
    mav.getModelMap().put("banco", banco);
    mav.addObject("estado", estadoService.buscarCodigo("01"));
    return mav;
  }

  @RequestMapping(value = "/Banco/new", method = RequestMethod.POST)
  public String createBanco(@ModelAttribute("banco") Banco banco, SessionStatus status) {
    bancoService.insertar(banco);
    status.setComplete();
    return "redirect:/pages/Banco/index";
  }

  @RequestMapping(value = "/Banco/edit", method = RequestMethod.GET)
  public ModelAndView editBanco(@RequestParam("idBanco") Integer idBanco) {
    ModelAndView mav = new ModelAndView("Banco/edit");
    Banco banco = bancoService.buscarIdBanco(idBanco);
    mav.getModelMap().put("banco", banco);
    mav.addObject("estado", estadoService.buscarCodigo("01"));
    return mav;
  }

  @RequestMapping(value = "/Banco/edit", method = RequestMethod.POST)
  public String updateBanco(@ModelAttribute("banco") Banco banco, SessionStatus status) {
    bancoService.actualizar(banco);
    status.setComplete();
    return "redirect:/pages/Banco/index";
  }

  @RequestMapping("/Banco/delete")
  public ModelAndView deleteBanco(@RequestParam("idBanco") Integer idBanco) {
    ModelAndView mav = new ModelAndView("redirect:/pages/Banco/index");
    Banco banco = bancoService.buscarIdBanco(idBanco);
    bancoService.eliminar(banco);
    return mav;
  }

  @RequestMapping(value = "/Banco/buscar", method = RequestMethod.POST)
  public ModelAndView buscarBancoNombre(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
    ModelAndView mav = new ModelAndView("Banco/resultadoBusqueda");
    log.info("banco = " + bancoService.buscarPorNombre(criterio.getNombre()).size());
    mav.getModel().put("banco", bancoService.buscarPorNombre(criterio.getNombre()));
    return mav;
  }
}